﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace mpsalaryprojectgroup
{
    public partial class NEWNEWReportUser : System.Web.UI.Page
    {
        String username = "";
        String loginName = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Usename"] != null)
            {
                username = (string)(Session["Usename"]);
                if (username == null)
                {
                    Response.Redirect("NEWMainPage.aspx");
                }
                else
                {
                    lblUsername.Text = username;
                }
            }
            else if (Session["UsenameLogin"] != null)
            {
                loginName = (string)(Session["UsenameLogin"]);
                if (loginName == null)
                {
                    Response.Redirect("NEWMainPage.aspx");
                }
                else
                {
                    lblUsername.Text = loginName;
                }
            }
            else
            {
                Response.Redirect("NEWMainPage.aspx");
            }


            DisableBufferingOnPage();
        }

        protected void DisableBufferingOnPage()
        {
            Response.Buffer = true;
            Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            // set expiry date in the past
            Response.Expires = -1;
            Response.CacheControl = "no-cache";
            Response.Cache.SetNoStore();
            Response.AppendHeader("Pragma", "no-cache");
            Response.AppendHeader("Cache-Control", "no-cache");
            Response.CacheControl = "no-cache";
            Response.Expires = -1;
            Response.ExpiresAbsolute = new DateTime(1900, 1, 1);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string dataString = "Server=.;Database=PGSPORTAL;Trusted_Connection=true"; //Data Source must change because of different computer names! //Janee\\sqlexpress
            SqlConnection sqlConnection = new SqlConnection(dataString);
            sqlConnection.Open();

            SqlCommand sqlCommand;

            int temp = Convert.ToInt32(ddlSalary.SelectedValue);
            temp = temp + 100;
            sqlCommand = new SqlCommand("select distinct year, course, freshgradsmonthlymedian, postgradsmonthlymedian from [Wages] WHERE isnumeric(FreshGradsMonthlyMedian) = 1 and CAST (replace(FreshGradsMonthlyMedian, ',', '') AS INT) > " + Convert.ToInt32(ddlSalary.SelectedValue) + "and CAST (replace(FreshGradsMonthlyMedian, ',', '') AS INT) < " + temp);
            //sqlCommand = new SqlCommand("select distinct year, course, freshgradsmonthlymedian, postgradsmonthlymedian from [Wages] WHERE CAST (FreshGradsMonthlyMedian AS INT) > 1600 and CAST (FreshGradsMonthlyMedian AS INT) < 1700");
            sqlCommand.Connection = sqlConnection;
            SqlDataReader dr = sqlCommand.ExecuteReader();
            
            
            
            
            //Label
            lblNotice.Visible = true;
            lblNotice.Text = "You've entered " + ddlSalary.Text + ". Students who graduated from the following courses will receive the same amount of salary.";
            //lblNotice.Text = Convert.ToString(temp);
            //Gridview here! Try databinding it. 
            gvCourseSalary.DataSource = dr;
            gvCourseSalary.DataBind(); //n.a or s values in the post grad median column.
            gvCourseSalary.Visible = true;
           
        }

        protected void btnBackToSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect("NEWNEWReport.aspx");
        }

        protected void gvCourseSalary_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[0].Text = "Year";
                e.Row.Cells[1].Text = "Course";
                e.Row.Cells[2].Text = "Monthly Median of Fresh Graduates";
                e.Row.Cells[3].Text = "Monthly Median of Post Graduates";
                
                

            }
        }

       
    }
}
//This form is the new form that evaluator told me to do. The user will select a salary range from the dropdown list and then a sentence with the 
//range of the salary which the user has selected will appear in a form of a label.
//The Gridview will display a list of courses which the median of the fresh graduates is RELEVANT to the salary range selected by the user.
//UNDERSTAND?!!! 